package com.mrlee.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrlee.bean.Admin;
import com.mrlee.bean.AdminRole;
import com.mrlee.bean.Role;
import com.mrlee.bean.RoleModule;
import com.mrlee.mapper.AdminMapper;
import com.mrlee.mapper.AdminRoleMapper;
import com.mrlee.mapper.RoleMapper;
import com.mrlee.service.AdminService;
import com.mrlee.utils.AjaxResult;
import com.mrlee.utils.StrUtils;
import com.mrlee.utils.TimeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 18/3/10.
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    private AdminMapper adminMapper;

    @Resource
    private AdminRoleMapper adminRoleMapper;

    @Resource
    private RoleMapper roleMapper;


    /**
     * 查询全部管理员
     * @return
     */
    @Override
    public AjaxResult selectAllAdmin() {
        AjaxResult result = new AjaxResult();
        List<Admin> adminList = adminMapper.selectAllAdmin();
        if (adminList.isEmpty()) {
            result.setCode("0");
            result.setMessage("查询失败");
            return result;
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(adminList);
        return result;
    }

    /**
     * 添加管理员
     * @param admin 管理员信息
     * @param roles 权限集合
     * @return
     */
    @Override
    public AjaxResult addAdmin(Admin admin, List<Integer> roles) {
        AjaxResult result = new AjaxResult();
        boolean isTel = StrUtils.isPhoneNumberRight(admin.getTelephone());
        boolean isEmail = StrUtils.isMailRight(admin.getEmail());
        boolean isName = StrUtils.isLetterNum_Right(admin.getName());
        boolean isPassword = StrUtils.isLetterNum_Right(admin.getPassword());
        boolean isCode = StrUtils.isLetterNum_Right(admin.getAdminCode());

        if (!(isTel && isEmail && isName && isPassword && isCode && (admin.getName().length() <= 20) && (admin.getAdminCode().length() <= 30) && (admin.getPassword().length() <= 30))) {
            result.setCode("0");
            result.setMessage("添加失败");
            return result;
        }

        admin.setEnrolldate(TimeUtils.time());
        int count = adminMapper.insert(admin);
        if (count == 0) {
            result.setCode("0");
            result.setMessage("添加失败");
            return result;
        }
        int id = admin.getAdminId();

        for (int i = 0; i < roles.size(); i++) {
            AdminRole adminRole = new AdminRole(id, roles.get(i));
            int countRole = adminRoleMapper.insert(adminRole);
            if (countRole == 0) {
                result.setCode("0");
                result.setMessage("添加失败");
                return result;
            }
        }
        result.setCode("1");
        result.setMessage("添加成功");
        return result;
    }

    /**
     * 删除管理员
     * @param adminId 管理员Id
     * @return
     */
    @Override
    public AjaxResult deleteAdmin(int adminId) {
        AjaxResult ajaxResult = new AjaxResult();
        int result = adminRoleMapper.deleteByAdminId(adminId);
        if (result == 0) {
            ajaxResult.setCode("0");
            ajaxResult.setMessage("删除失败");
            return ajaxResult;
        }
        int count = adminMapper.deleteByPrimaryKey(adminId);
        if (count == 0) {
            ajaxResult.setCode("0");
            ajaxResult.setMessage("删除失败");
            return ajaxResult;
        }
        ajaxResult.setCode("1");
        ajaxResult.setMessage("删除成功");
        return ajaxResult;
    }

    /**
     * .根据Id查找管理员
     * @param adminId 管理员Id
     * @return
     */
    @Override
    public Admin selectAdminById(int adminId) {
        Admin admin = adminMapper.selectByPrimaryKey(adminId);
        List<Role> aList = admin.getRoleList();
        for (int i = 0; i < aList.size(); i++) {
            Role role = aList.get(i);
            Role rid = roleMapper.findIdByName(role.getName());
            role.setRoleId(rid.getRoleId());
        }
        admin.setRoleList(aList);
        return admin;
    }

    /**
     * 修改管理员
     * @param admin 管理员信息
     * @param roles 权限集合
     * @return
     */
    @Override
    public AjaxResult updateAdmin(Admin admin, List<Integer> roles) {
        AjaxResult result = new AjaxResult();
        boolean isTel = StrUtils.isPhoneNumberRight(admin.getTelephone());
        boolean isEmail = StrUtils.isMailRight(admin.getEmail());
        boolean isName = StrUtils.isLetterNum_Right(admin.getName());

        if (!(isTel && isEmail && isName && (admin.getName().length() <= 20))) {
            result.setCode("0");
            result.setMessage("修改失败");
            return result;
        }

        admin.setEnrolldate(TimeUtils.time());
        int count = adminMapper.updateByPrimaryKey(admin);
        if (count == 0) {
            result.setCode("0");
            result.setMessage("修改失败");
            return result;
        }


        int id = admin.getAdminId();

        adminRoleMapper.deleteByAdminId(id);

        for (int i = 0; i < roles.size(); i++) {
            AdminRole adminRole = new AdminRole(id, roles.get(i));
            int countRole = adminRoleMapper.insert(adminRole);
            if (countRole == 0) {
                result.setCode("0");
                result.setMessage("修改失败");
                return result;
            }
        }
        result.setCode("1");
        result.setMessage("修改成功");
        return result;
    }

    /**
     * 重置密码
     * @param admins 管理员Id集合
     * @return
     */
    @Override
    public AjaxResult resetPwd(List<Integer> admins) {
        AjaxResult result = new AjaxResult();
        for (int adminId : admins) {
            int num = adminMapper.updatePwdById(adminId);
            if (num < 1) {
                result.setCode("0");
                result.setMessage("重置失败");
                return result;
            }
        }
        result.setCode("1");
        result.setMessage("重置成功");
        return result;
    }

    /**
     * 搜索
     * @param roleName 角色名
     * @param name 姓名
     * @return
     */
    @Override
    public AjaxResult search(String roleName, String name) {
        AjaxResult result = new AjaxResult();

        List<Admin> adminList = new ArrayList<Admin>();
        if ("全部".equals(roleName)) {
            if ("".equals(name)) {
                adminList = adminMapper.selectAllAdmin();
            } else {
                adminList = adminMapper.selectAdminByName(name);
            }

        } else {
            List<Admin> admins = adminMapper.selectAdminByRoleName(roleName);
            List<Admin> adminsByName = adminMapper.selectAdminByName(name);
            if ("".equals(name)) {
                adminList = admins;
            } else {
                for (Admin admin : admins) {
                    for (Admin adminByName : adminsByName){
                        if (admin.getName().equals(adminByName.getName())) {
                            adminList.add(admin);
                        }
                    }
                }
            }
        }
        result.setCode("1");
        result.setMessage("查询成功");
        result.setData(adminList);
        return result;
    }

    /**
     * 根据登录名查找管理员
     * @param name 登录名
     * @return
     */
    @Override
    public Admin selectAdminByUserame(String name) {
        return adminMapper.selectAdminByUserame(name);
    }

    /**
     * 修改管理员
     * @param admin 管理员信息
     * @return
     */
    @Override
    public AjaxResult updateAdminInfo(Admin admin) {
        AjaxResult result = new AjaxResult();
        boolean isName = StrUtils.isChineseLetterNumRight(admin.getName());
        boolean isTel = StrUtils.isPhoneNumberRight(admin.getTelephone());
        boolean isEmail = StrUtils.isMailRight(admin.getEmail());

        if (!(isName&&isTel&&isEmail&&(admin.getName().length()<20))){
            result.setCode("0");
            result.setMessage("修改信息格式不正确");
            return result;
        }

        int num = adminMapper.updateByPrimaryKeySelective(admin);
        if (num < 1){
            result.setCode("0");
            result.setMessage("修改失败");
        }
        result.setCode("1");
        result.setMessage("修改成功");
        return result;
    }

    /**
     * 分页
     * @param pageNo 当前页
     * @param pageSize 每页显示信息数
     * @return
     */
    @Override
    public PageInfo<Admin> queryAdminByPage(Integer pageNo, Integer pageSize) {
        pageNo = pageNo == null ? 1 : pageNo;
        pageSize = pageSize == null ? 10 : pageSize;

        //这句话一定写在查询之前
        PageHelper.startPage(pageNo,pageSize);
        List<Admin> adminList = adminMapper.selectAllAdmin();
        PageInfo<Admin> adminPageInfo = new PageInfo<Admin>(adminList);
        return adminPageInfo;
    }


    @Override
    public Integer updateByName(String name, String password) {
        return adminMapper.updateByName(name,password);
    }


}
